<?php

namespace App\Http\Controllers\Home\Dream\Activity;

use App\Api\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Models\Dream\Activity\Sphere\Content;
use App\Models\Dream\Activity\Sphere\UserCode;
use App\Http\Controllers\Api\Wechat\JsSdkController;
use App\Models\User;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;

class SphereController extends Controller
{

    /**
     * 双色球活动
     */

    use ApiResponse;

    //活动展示
    public function index()
    {
        $content_data = Content::orderBy('id','desc')->paginate('10');

        return view('home.activity.sphere.index')->with('data',$content_data);
    }

    //修改
    public function edit(Request $request)
    {
        $id = $request->id;
        $data = Content::where('id',$id)->first();

        return view('home.activity.sphere.edit')->with('data',$data);
    }

    //更新
    public function update(Request $request){

        $id = $request->id;
        $code = $request->code;

        $update = Content::where('id',$id)->update([
            'code' => $code
        ]);

        return $this->success($update);
    }

    //创建
    public function create()
    {
        return view('home.activity.sphere.create');
    }

    //更新
    public function store(Request $request)
    {
       $data['start_time'] = $request->start;
       $data['stop_time'] = $request->stop;
       $data['prize_name'] = $request->prize_name;
       $data['prize_title'] = $request->prize_title;
       $data['prize_image'] = $request->prize_image;

       $creat = Content::create($data);
       return $this->success($creat);
    }

    //消息推送（消息订阅）
    public function pushMsg(JsSdkController $jsSdkController,Request $request)
    {
        $id = $request->id;
        //获取openid
        $user_id = Content::where('id',$id)->value('user_id');
        $openid = User::where('id',$user_id)->value('weixin_openid');


        if(empty($user_id)){
           return $this->setStatusCode('201')->success('抽奖还未结束','error');
        }

            //获取微信 access_token
        $access_token = $jsSdkController->getAccessToken();

        $content_data = Content::where('id',$id)->first();

        $new_time = strtotime($content_data['stop_time']);//日期转时间戳：返回的日期为"Y-m-d"格式，这里需要转为"Ymd "格式

        $date = date('Ymd',$new_time);

        $time =  date('Y'.'年'.'m'.'月'.'d',time());//当前时间

        if ($openid){
            Log::info('微信 openid:'.$openid);
        }else{
            Log::info('微信 openid null');
        }

        //消息推送
        //设置接口参数
        $datas = [
            'template_id' => 'duJ3swThmJtemkxxKi-rbXd4P1iWD5Qzf-6l43mCzvM',//所需下发的订阅模板id zopuAFruew30eoWU5JbHCU8kWdfYZGrEhut_i6WMk70
            'touser' => $openid,//接收者（用户）的 openid
            'page' => '',//不跳转
            'data' => [

                'name1' => [
                    'value' => $content_data['prize_name'],//奖品名称
                ],
                'number4' => [
                    'value' => $date,//活动期数
                ],
                'date3' => [
                    'value' => $time,
                ],
                'thing7' => [
                    'value' => '恭喜中奖，后续管理员会与您取得联系',//温馨提示
                ],
            ],
        ];

        $client = new Client(['base_uri' => 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$access_token]);

//        $client = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.31.XX:xxx/api/']);

        $itemsResponse = $client->request('post','',
            ['json' => $datas,
                'headers' => [
                    'Content-type'=> 'application/json',
//                'Cookie'=> 'XDEBUG_SESSION=PHPSTORM',
                    "Accept"=>"application/json"]
            ]);

        $data = $itemsResponse->getBody()->getContents();

        if ($data){
            Log::info($data);
            return $this->success('');
        }else{
            Log::info('消息推送失败');
        }


    }

}
